#!/usr/bin/env bash
#
# What  : A script to transform XML files into RDF.
#
# Usage :
#
#  $ murics2rdf.sh <sites.cfg>
#
#  - <sites.cfg> is a mandatory site configuration file, where each line
#    specifies the directory of the input XML files for a site.
#    When not specified, the default one from ${BASE_DIR}/sites.cfg is used.
#
#    Each line of <sites.cfg> contains 2 tokens:
#
#   - token 1 - mandatory:
#
#     Full path to the directory containing all the XML files to be transformed
#     for a given site.
#     The script assumes the final directory name is the site's name.
#
#     eg. given /data/site_dirs/iccr/, 'iccr' is assumed to be the site's name
#
#   - token 2 - optional:
#
#     The XSLT to apply to each XML file in the above directory.
#     When not specified, the default one from $DEFAULT_XSLT is used
#
# Author: King Lung Chiu
# ==========================================================================
SELF=${0##*/}
TIME_STAMP=`date +%Y-%m-%d.%k%M%S`
BKUP_BASE_DIR=backups

# location of the core script directories
BASE_DIR="$( cd "$( dirname "$0" )" && pwd )"
TOOLS_DIR=${BASE_DIR}/tools
XSLT_DIR=${BASE_DIR}/xslt
SAXON_HOME=${TOOLS_DIR}/saxon

DEFAULT_CFG=${BASE_DIR}/sites.cfg
SITES_CFG=${1:-${DEFAULT_CFG}}
DEFAULT_XSLT=${XSLT_DIR}/murifcs2rdf.xsl

# quit with usage info if a site configuration file can't be found
if ! [ -f $SITES_CFG ]
then
   echo
   echo " usage: $ ${SELF} <sites.cfg>"
   echo
   echo "  - <sites.cfg> = site configuration file"
   echo "    if not specified, the default one from ${DEFAULT_CFG} is used"
   echo
   exit 1
fi

echo
# read a site's directory and XSLT from each line of the site configuration file
while read SITE_DIR SITE_XSLT
do
   echo " site dir     : ${SITE_DIR}"

   # skip if SITE_DIR is not a directory
   if ! [ -d "$SITE_DIR" ]; then
      echo " action       : skipping - directory doesn't exist!"
      #echo "skipping '$SITE_DIR': not a directory!"
      echo
      continue
   fi

   # save current location
   pushd "$SITE_DIR" > /dev/null

   SITE="${SITE_DIR##*/}"
   SITE_BKUP_DIR="${BKUP_BASE_DIR}.$SITE"
   SITE_BKUP_LOG="${SITE_BKUP_DIR}/${SITE_BKUP_DIR}.log"
   BKUP_DIR="${SITE_BKUP_DIR}/${TIME_STAMP}"
   SITE_XSLT=${SITE_XSLT:-${DEFAULT_XSLT}}
   #echo "site dir     : ${SITE_DIR}"
   echo " action       : inspecting"
   echo " site bkup dir: ${PWD}/${SITE_BKUP_DIR}/"

   # create the site's backup dir & log file if they don't already exist
   test -d $SITE_BKUP_DIR || mkdir -p $SITE_BKUP_DIR
   test -e $SITE_BKUP_LOG || touch $SITE_BKUP_LOG

   # inspect every xml file in SITE_DIR
   let TOTAL_FILES=0
   let PROCESSED_FILES=0
   #find ./ -maxdepth 1 -nowarn -name "*.xml" -type f | while read INPUT_FILE
   for INPUT_FILE in ./*.xml
   do
      test -f "$INPUT_FILE" || continue

      let TOTAL_FILES++
      INPUT_FILE="${INPUT_FILE#./}"
      OUTPUT_FILE="${INPUT_FILE%.xml}.rdf"
      LAST_LOG=`grep -h "$INPUT_FILE" "$SITE_BKUP_LOG" | tail -1`
      LAST_BKUP_TIME="${LAST_LOG#* }"
      LAST_BKUP="${SITE_BKUP_DIR}/${LAST_BKUP_TIME}/${INPUT_FILE}"

      # If INPUT_FILE is new:
      #  - transform it with SITE_XSLT,
      #  - back up both INPUT_FILE and its transform output (OUTPUT_FILE)
      #  - log the operation.
      #
      # INPUT_FILE is new if:
      #  - it has no prior backup (ie. no log entry), or
      #  - it differs from its latest backup
      if [ -z "$LAST_LOG" ] || ! `cmp -s "$INPUT_FILE" "$LAST_BKUP"`
      then
         let PROCESSED_FILES++
         echo "  + Processing new file: $INPUT_FILE"

         # feed INPUT_FILE to Saxon with SITE_XSLT
         # individually quote parameters as below if expecting spaces in file names
         #${TRANSFORM_CMD} " $INPUT_FILE" "$SITE_XSLT" "$OUTPUT_FILE"
         #java -cp ${CLASS_PATH} ${SAXON_CLASS} -s:${INPUT_FILE} -xsl:${SITE_XSLT} -o:${OUTPUT_FILE}
         $SAXON_HOME/saxon.sh ${INPUT_FILE} ${OUTPUT_FILE} ${SITE_XSLT}

         # backup INPUT_FILE & OUTPUT_FILE to BKUP_DIR
         mkdir -p "$BKUP_DIR"
         cp "$INPUT_FILE" "${BKUP_DIR}/${INPUT_FILE}"
         cp "$OUTPUT_FILE" "${BKUP_DIR}/${OUTPUT_FILE}"

         # log the operation (not for displaying)
         echo "$INPUT_FILE $TIME_STAMP" >> "$SITE_BKUP_LOG"
      else
         echo "  - Skipping old file  : $INPUT_FILE. Last backup timestamp = $LAST_BKUP_TIME"
      fi
   done

   echo "  total .xml files     : $TOTAL_FILES"
   echo "  processed .xml files : $PROCESSED_FILES"
   echo

   # restore original location
   popd > /dev/null
done < $SITES_CFG